package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 积分赠送记录
 */
@TableBind(tableName="point_give_log")
public class PointGiveLog extends BasicModel<PointGiveLog> {
    public static final PointGiveLog dao = new PointGiveLog();
    public static final String SELECT_ALL = "`from_user_id`,`from_user_name`,`to_user_id`,`to_user_name`,`create_time`,`record_id`";

    @Override
    public Page<PointGiveLog> paginate(Integer pageNumber, Integer pageSize, String column, PointGiveLog model) {
        List<Object> paras = new ArrayList<>();
        StringBuffer sqlExceptSelectBuffer = new StringBuffer("FROM " + getTableName() + " WHERE 1=1");
        StringBuffer selectBuffer = new StringBuffer("SELECT ").append(column);
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        } else {
            pageNumber++;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (model != null) {
            String[] attrNames = model.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = model.get(attrName);
                if (attrValue != null) {
                    if(attrName.equals("from_user_name")){
                        sqlExceptSelectBuffer.append(" AND `from_user_name` LIKE ? ");
                        attrValue = "%"+attrValue+"%";
                    }else if(attrName.equals("to_user_name")){
                        sqlExceptSelectBuffer.append(" AND `to_user_name` LIKE ? ");
                        attrValue = "%"+attrValue+"%";
                    }else if(attrName.equals("start_create_time")){
                        sqlExceptSelectBuffer.append(" AND `create_time` >= ? ");
                    }else if(attrName.equals("end_create_time")){
                        sqlExceptSelectBuffer.append(" AND `create_time` <= ? ");
                    }else{
                        sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    }
                    paras.add(attrValue);
                }
            }
        }

        if (paras.size() > 0) {
            return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }
        return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
    }
            
    public PointGiveLog setFromUserId(Long fromUserId){
        return this.set("from_user_id",fromUserId);
    }

    public Long getFromUserId(){
        return this.get("from_user_id");
    }
        
    public PointGiveLog setFromUserName(String fromUserName){
        return this.set("from_user_name",fromUserName);
    }

    public String getFromUserName(){
        return this.getStr("from_user_name");
    }
        
    public PointGiveLog setToUserId(Long toUserId){
        return this.set("to_user_id",toUserId);
    }

    public Long getToUserId(){
        return this.get("to_user_id");
    }
        
    public PointGiveLog setToUserName(String toUserName){
        return this.set("to_user_name",toUserName);
    }

    public String getToUserName(){
        return this.getStr("to_user_name");
    }
        
    public PointGiveLog setCreateTime(Date createTime){
        return this.set("create_time",createTime);
    }

    public Date getCreateTime(){
        return this.getDate("create_time");
    }
        
    public PointGiveLog setRecordId(Long recordId){
        return this.set("record_id",recordId);
    }

    public Integer getRecordId(){
        return this.get("record_id");
    }
}